package com.cvit.cn.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.cvit.cn.bean.Commodity;
import com.cvit.cn.bean.Msg;
import com.cvit.cn.dao.CommodityMapper;
import com.cvit.cn.test.ExcelUtil;
import com.cvit.cn.test.Utils;
//import com.cvit.cn.test.ExcelUtil;

@Service
public class CommodityService {
	@Autowired
	CommodityMapper commodityMapper;
	
	/*查询所有*/
	public List<Commodity> getAll() {
		// TODO Auto-generated method stub
		return commodityMapper.selectByExample(null);
	}
	
	/**
	 * 根据releaseId和commenttype=1查询
	 * */
	public List<Commodity> getReleaseId(String releaseId) {
		// TODO Auto-generated method stub
		return commodityMapper.selectByReleaseId(releaseId);
	}
	/**
	 * 根据releaseId和commenttype=0查询
	 * */
	public List<Commodity> getReleaseIds(String releaseId) {
		// TODO Auto-generated method stub
		return commodityMapper.selectByReleaseIds(releaseId);
	}
	
	/*public List<Commodity> releaseId() {
		// TODO Auto-generated method stub
		return commodityMapper.seleReleaseId(null);
	}*/

	/**
	 * 导入excel表格
	 * */
	public int importExcel(InputStream in, MultipartFile file,String releaseId) throws Exception {
		// TODO Auto-generated method stub
		List<List<Object>> listob=ExcelUtil.getBankListByExcel(in, file.getOriginalFilename());
		//遍历listob数据
		for(int i=0;i<listob.size();i++){
			List<Object> list=new ArrayList<Object>();
			List<Object> object=listob.get(i);
			if(object.get(0).equals(releaseId)){
				Commodity commodity=new Commodity();
				commodity.setId(Utils.getsUUID());
				commodity.setReleaseId(String.valueOf(object.get(0)));  //发布信息id
				commodity.setOneId(String.valueOf(object.get(1)));       //id1
				commodity.setTwoId(String.valueOf(object.get(2)));        //id2
				commodity.setDepartment(String.valueOf(object.get(3)));  //部门
				commodity.setCommodityName(String.valueOf(object.get(4)));  //物品名称
				//commodity.setCommodityCategory(String.valueOf(object.get(5)));  //物品类别
				commodity.setSpecificationModel(String.valueOf(object.get(5)));  //物品型号
				commodity.setCompany(String.valueOf(object.get(6)));           //单位
				commodity.setNumber(String.valueOf(object.get(7)));            //数量
				commodity.setCommodityCategory(String.valueOf(object.get(8)));  //备注
				commodity.setCommodityType("0");
				commodityMapper.insertExcel(commodity); 
			}else{
				return 2;
			}
//			 boolean commodityId = getReleaseById(String.valueOf(object.get(0)));
//		        if(commodityId==false){
//		        	commodityMapper.deleteByReleaseId(commodity.getReleaseId());
//		        }
		}
		return 1;
		
	}

	/**
	 * 添加物品信息
	 * */
	public void addComm(Commodity commodity) {
		// TODO Auto-generated method stub
		String uuid=Utils.getsUUID().toString();
		Commodity c=new Commodity();
		c.setId(uuid);
		//t.setStarlTime(time.getStarlTime());
		c.setReleaseId(commodity.getReleaseId());
		c.setCommodityName(commodity.getCommodityName());
		c.setCommodityCategory(commodity.getCommodityCategory());
		c.setSpecificationModel(commodity.getSpecificationModel());
		c.setCompany(commodity.getCompany());
		c.setNumber(commodity.getNumber());
		commodityMapper.insertComm(c);
		
	}

	/**
	 * 修改物品表
	 * */
	public void updataComm(Commodity commodity) {
		// TODO Auto-generated method stub
		commodityMapper.updataComm(commodity);
		
	}

	/**
	 * 按id批量删除
	 * */
	public int deleCommId(String[] ids) {
		return commodityMapper.deleCommId(ids);
	}

	/**
	 * 根据commodityType=1查询
	 * */
	public List<Commodity> getCommodityByType() {
		// TODO Auto-generated method stub
		List<Commodity> commoditys = commodityMapper.selectByType(null);
		return commoditys;
	}

	/**
	 * 把状态值都改成commodityType=0
	 * */
	public void updataType(Commodity comm) {
		// TODO Auto-generated method stub
		commodityMapper.updataType(comm);
		
	}

	/**
	 * 根据ReleaseId给commodityTye赋值为1
	 * */
	public void updataReleaseId(Commodity comm) {
		// TODO Auto-generated method stub
		commodityMapper.updataReleaseId(comm);
	}

	/**
	 * 按照release删除物品
	 * */
	public void deleReleaseId(String releaseId) {
		// TODO Auto-generated method stub
		commodityMapper.deleReleaseId(releaseId);
	}
}
